<template>
  <div>
    <bread second="权限管理" third="角色列表"></bread>
    <el-card class="box-card" style="margin-top:15px">
      <el-button type="primary" @click="dialogFormVisible = true">添加角色</el-button>
      <el-table :data="tableData" border style="width: 100%;margin-top:10px">
        <el-table-column type="expand">
          <template slot-scope="scoped">
            <div
              v-for="(item1, index1) in scoped.row.children"
              :key="index1"
              style="border-bottom:1px solid #eee;"
            >
              <el-row>
                <el-col :span="8">
                  <div>
                    <!-- 左边 一级 -->
                    <el-tag type="success" closable style="margin:10px 0">{{item1.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </div>
                </el-col>
                <el-col :span="14">
                  <div>
                    <!-- 右边 -->
                    <el-row v-for="(item2, index2) in item1.children" :key="index2">
                      <el-col :span="8">
                        <div>
                          <!-- 右边的左边 二级-->
                          <el-tag type="warning" closable style="margin:10px 0">{{item2.authName}}</el-tag>
                          <i class="el-icon-caret-right"></i>
                        </div>
                      </el-col>
                      <el-col :span="14">
                        <div>
                          <!-- 右边的右边 三级-->
                          <el-tag
                            type="danger"
                            closable
                            v-for="(item3, index3) in item2.children"
                            :key="index3"
                            style="margin:10px 0"
                          >{{item3.authName}}</el-tag>
                        </div>
                      </el-col>
                    </el-row>
                  </div>
                </el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="#" width="50px"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column prop="mobile" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="get(scope.row)">
              <i class="el-icon-edit"></i>编辑
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">
              <i class="el-icon-delete"></i>删除
            </el-button>
            <el-button type="warning" size="mini" @click="chosePower(scope.row)">
              <i class="el-icon-setting"></i>分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加角色 -->
    <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="roleForm">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" :label-width="formLabelWidth">
          <el-input v-model="form.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑角色 -->
    <el-dialog title="编辑角色" :visible.sync="dialogFormVisible2">
      <el-form :model="form" ref="roleForm">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" :label-width="formLabelWidth">
          <el-input v-model="form.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="editRole">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配权限 -->
    <el-dialog title="分配权限" :visible.sync="dialogFormVisible3">
      <el-form :model="form">
        <el-tree
          :data="data"
          show-checkbox
          node-key="id"
          ref="tree"
          :default-expanded-keys="[2, 3]"
          :default-checked-keys="defaultKey"
          :props="defaultProps"
          :default-expand-all="true"
        ></el-tree>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible3 = false">取 消</el-button>
        <el-button type="primary" @click="choseHandler">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
/* eslint-disable */
import {
  getRoleList_api,
  delRole_api,
  addRole_api,
  editRole_api,
  choseHandler_api,
  getRightList_api,
} from '@/api/index.js'
import loginVue from '../login.vue'
export default {
  data() {
    return {
      tableData: null,
      dialogFormVisible: false,
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      form: {
        roleName: '',
        roleDesc: '',
        id: '',
      },
      formLabelWidth: '120px',
      data: [],// 显示数状的数据
      id: '',// 当前角色id
      defaultKey: [],// 显示默认权限，并且是第三级的id值
      defaultProps: {
        children: 'children',
        label: 'authName'
      }

    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const { data: res } = await getRoleList_api()
      console.log(res, 1);
      this.tableData = res.data
    },
    // 添加角色
    async addRole() {
      const { data: res } = await addRole_api(this.form)
      // console.log(res, 0);
      if (res.meta.status == 201) {
        this.$message({
          message: res.meta.msg,
          type: 'success'
        })
        this.dialogFormVisible = false
        this.getRoleList()
        //清空表单数据
        // console.log(this.$refs.roleForm, 0);
        this.$refs.roleForm.resetFields();
      }

    },
    // 删除角色
    async handleDelete(row) {
      const res2 = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
      if (res2 == 'confirm') {
        const { data: res } = await delRole_api(row.id)
        // console.log(res, 123);
        this.getRoleList()
      }
    },
    // 编辑角色
    get(row) {
      this.dialogFormVisible2 = true
      this.form = row
    },
    async editRole() {
      const { data: res } = await editRole_api(this.form.id, { roleName: this.form.roleName, roleDesc: this.form.roleDesc })
      if (res.meta.status === 200) {
        this.$message({
          message: '修改角色成功',
          type: 'success'
        })
        this.dialogFormVisible2 = !this.dialogFormVisible2
        this.getRoleList()
      }
    },
    // 分配权限
    // 递归函数找row中的最底层的children中的id
    digui(row, arr) {
      if (!row.children) {
        return arr.push(row.id)
      }
      row.children.forEach(item => {
        this.digui(item, arr)
      })
    },
    // 获取树形权限数据
    async chosePower(row) {
      //每次点进去清空上一次默认选中状态
      this.defaultKey = []
      this.dialogFormVisible3 = true
      const { data: res } = await getRightList_api('tree')
      this.data = res.data
      this.id = row.id
      //调用递归函数找row中的最底层的children中的id,设置为默认选中状态
      this.digui(row, this.defaultKey)
    },
    // 确认修改分配权限
    async choseHandler() {
      // 当前被全选中和半选中的id
      var rids = [...this.$refs.tree.getCheckedKeys(), ...this.$refs.tree.getHalfCheckedKeys()]
      var rid = rids.join(',')
      const { data: res } = await choseHandler_api({ roleId: this.id, rids: rid })
      console.log(res, rid, this.id, 'aa');
      if (res.meta.status === 200) {
        this.$message({
          message: res.meta.msg,
          type: 'success'
        })
        this.dialogFormVisible3 = false
        this.getRoleList()
      }
    }
  },
}
</script>
<style lang="less" scoped>
.box-card {
  width: 100%;
}
</style>

